using System;
using Castle.Components.Validator;

namespace SimplePMT.Components.ActiveRecordExtensions
{
	public class NullOrMaxValidator : AbstractValidator
	{
		private int _maxlength;
		/// <summary>
		/// Initializes a new instance of the NullOrMaxValidator class.
		/// </summary>
		/// <param name="maxlength"></param>
		public NullOrMaxValidator(int maxlength)
		{
			_maxlength = maxlength;
		}
		public override bool IsValid(object instance, object fieldValue)
		{
			if (fieldValue == null)
				return true;

			if (fieldValue.ToString().Length <= _maxlength)
				return true;

			return false;
		}

		public override bool SupportsBrowserValidation
		{
			get { return false; }
		}
	}
}
